from flask import url_for, request
from flask_admin.contrib.sqla import ModelView
from flask_login import current_user
from werkzeug.utils import redirect

# 继承ModelView,实现一些自定义扩展
from models import User


class MyModelView(ModelView):
    can_delete = True
    can_create = False  # 是否能创建
    # Override displayed fields
    column_list = ('name', 'age')  # 显示的属性
    # 重写方法实现权限控制
    def is_accessible(self):
        print(current_user.name)  #  获取login的当前用户
        if current_user.is_authenticated and current_user.name == "Tom":
            return True
        return False
    # 访问页面没有权限时回调函数,可跳转登录
    def inaccessible_callback(self, name, **kwargs):
        # redirect to login page if user doesn't have access
        return redirect(url_for('myuser.login', next=request.url))


from flask_admin import Admin, expose, BaseView

# 继承BaseView进行视图页面定制
class UserView(BaseView):
    # 使用expose进行路由,每个视图必须有一个 '/' 函数,否则报错
    @expose('/')
    def index(self):
        return self.render('boot.html')

    @expose('/user_manager')
    def user_manager(self):
        return self.render('boot.html')



